from httpfpt.common.log import log
from httpfpt.core import get_conf

if __name__ == "__main__":
    host_ip = get_host_ip()
    print("Host IP: {}".format(host_ip))

class QiweiTalk:
    def __init__(self, content: dict):
        self.content = content

    def send(self) -> None:
        try:
            import requests
            headers = {'Content-Type': 'application/json; charset=utf-8', 'Connection': 'close'}
            report_url = self.content['report_url']
            data = {
                'msgtype': 'markdown',
                'markdown': {
                    # 'title': get_conf.TEST_REPORT_TITLE,
                    'title': '自动化用例',
                    'content': f"> ## BI-移动端 自动化测试报告\n\n"
                               f"> 🤖 测试结果: {self.content['result']}\n\n"
                               f"> ✅ 通过用例: {self.content['passed']}\n\n"
                               f"> 🔧 失败用例: {self.content['failed']}\n\n"
                               f"> ❌ 错误用例: {self.content['error']}\n\n"
                               f"> ⚠️ 跳过用例: {self.content['skipped']}\n\n"
                               f"> ⌛ 开始时间: {self.content['started_time']}\n\n"
                               f"> ⏱️ 执行耗时: {self.content['elapsed']} s\n\n"
                               f"> ➡️ [查看详情](192.168.2.205:10001/{report_url})",
                    # f"> ➡️ [查看详情](https://foryourself)",
                },
            }
            response = requests.session().post(
                url=get_conf.QIWEI_TALK_WEBHOOK, json=data, headers=headers  # type: ignore  # noqa: E501
            )
            response.raise_for_status()
            print(response.request.body)
            print(response.json())
        except Exception as e:
            log.error(f'企微消息发送异常: {e}')
        else:
            log.success('企微消息发送成功')